package com.rj.bd.service.solr;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.rj.bd.entity.Book;
import com.rj.bd.entity.Class1;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author FTY
 * @desc
 * @time 2021-05-07-10:18
 */
@Component
public class BookSolr {

//    @Autowired  （过时方法）
//    private static SolrServer solrServer;

    @Autowired
    private SolrClient solrClient;
    
    /**
     * 查询全部
     * @return
     */
    public   List<Book> queryAll(Map maps, String name) throws SolrServerException, IOException {

        System.out.println("pageNo"+maps.get("pageNo")+"\t"+"pageSize"+maps.get("pageSize")+"\t"+"name"+name);
        int pageNo=maps.get("pageNo")==null?1:Integer.valueOf(maps.get("pageNo")+"");
        int pageSize=maps.get("pageSize")==null?100:Integer.valueOf(maps.get("pageSize")+"");

        SolrQuery solrQuery = new SolrQuery();

        if(name != null){//设置查询条件
            solrQuery.setQuery("bname:*"+name+"*");
        }
        else{
            solrQuery.setQuery("*:*");
        }
//        StringBuilder stringBuilder = new StringBuilder();
//
//        if(name != null && !name.equals("")){//设置查询条件
//            System.out.println("bname=" + name);
//            stringBuilder.append("bname:*"+name+"*");
//        }
//        else{
//            System.out.println("bname=" + name);
//            stringBuilder.append("*:*");
//        }


//        System.out.println("stringBuilder = " + stringBuilder.toString());
//        String set = stringBuilder.toString();
//        solrQuery.setQuery(set);
        
        //开始行
        solrQuery.setStart(pageNo-1);
        //数量
        solrQuery.setRows(pageSize);
        System.out.println("测试111");
        //执行查询
        QueryResponse response = solrClient.query(solrQuery);
        System.out.println("测试222");
        //结果集
        SolrDocumentList solrDocument = response.getResults();
        System.out.println("测试333");
        // 总条数
        long docNum = solrDocument.getNumFound();
        System.out.println("一共有多少个文档对象:"+docNum);
        System.out.println("solrDocument的长度为:"+solrDocument.size());
        List<Book> book = new ArrayList();

        for (SolrDocument doc : solrDocument) {

            //打印
            System.out.println("id:"+doc.get("id"));
            System.out.println("bname:"+doc.get("bname"));
            System.out.println("bwriter:"+doc.get("bwriter"));
            System.out.println("bconcern:"+doc.get("bconcern"));
            System.out.println("bnum:"+doc.get("bnum"));
            System.out.println("bprice:"+doc.get("bprice"));
            System.out.println("bimg:"+doc.get("bimg"));
            System.out.println("zhuangtai:"+doc.get("zhuangtai"));
            System.out.println("cid:"+doc.get("cid"));
            System.out.println("cname:"+doc.get("cname"));


            //封装
            Book book1 = new Book();
            Class1 class1=new Class1();
            book1.setBid(doc.get("id").toString());
            book1.setBname(doc.get("bname").toString());
            book1.setBwriter(doc.get("bwriter").toString());
            book1.setBconcern(doc.get("bconcern").toString());
            book1.setBnum(doc.get("bnum").toString());
            book1.setBprice(doc.get("bprice").toString());
            book1.setBimg(doc.get("bimg").toString());
            book1.setZhuangtai(doc.get("zhuangtai").toString());
            class1.setCid(doc.get("cid").toString());
            class1.setCname(doc.get("cname").toString());
            book1.setClass1(class1);
            book.add(book1);
        }
        return book;
    }
}
